package com.example.jn_dxs.Ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.jn_dxs.Base.BaseActivity;
import com.example.jn_dxs.Base.BaseViewModel;
import com.example.jn_dxs.Bean.PopuBean;
import com.example.jn_dxs.Bean.SearchBean;
import com.example.jn_dxs.R;
import com.example.jn_dxs.Ui.adapter.SearchAdapter;
import com.example.jn_dxs.Ui.adapter.StatisticsAdapter;
import com.example.jn_dxs.Utlis.LoadingUtil;
import com.example.jn_dxs.ViewModel.SearchVM;
import com.example.jn_dxs.databinding.ActivityShearchBinding;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends BaseActivity<SearchVM, ActivityShearchBinding> {

    private SearchAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shearch;
    }

    @Override
    protected void initEventAndView() {
        binding.MyTopBar.tvBack.setOnClickListener(v -> finish());
        binding.MyTopBar.tvTitle.setText("设备列表");
        adapter = new SearchAdapter(R.layout.item_fragment_statistics);
        binding.SearchList.setAdapter(adapter);
        adapter.setListenter((view, popuBean, pos) -> {
            //数据展示
            if (getIntent().getIntExtra("type", -1) != -1) {
                switch (getIntent().getIntExtra("type", -1)) {
                    case 1:
                        startActivity(new Intent(this, SingleWellWaterTemperatureActivity.class).putExtra("id",popuBean.getPkiaa()));
                        break;
                    case 2:
                        startActivity(new Intent(this, WaterTemperatureTableActivity.class).putExtra("id",popuBean.getPkiaa()));
                        break;
                    case 3:
                        startActivity(new Intent(this, WaterTemperatureTrendActivity.class).putExtra("id",popuBean.getPkiaa()));
                        break;
                    case 4:
                        startActivity(new Intent(this, SingleWellWaterLevelActivity.class).putExtra("id",popuBean.getPkiaa()));
                        break;
                    case 5:
                        startActivity(new Intent(this, WaterLevelStatisticsActivity.class).putExtra("id",popuBean.getPkiaa()));
                        break;
                    case 6:
                        startActivity(new Intent(this, WaterLevelTrendActivity.class).putExtra("id",popuBean.getPkiaa()));
                        break;
                }
            }
        });
        binding.searchET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                model.getSearch(s.toString());
            }
        });
        LoadingUtil.setStatus(this, 1);
        model.getSearch(binding.searchET.getText().toString());
    }


    @Override
    public void initClick() {
        binding.SearchTV.setOnClickListener(v -> {
            model.getSearch(binding.searchET.getText().toString());
        });
    }

    @Override
    protected void onObjectDataChange(JSONObject object) {
        super.onObjectDataChange(object);
        LoadingUtil.hide();
        JSONArray rows = object.getJSONArray("rows");
        int total = object.getIntValue("total");
        List<SearchBean> list = new ArrayList<>();
        if (total > 0) {
            for (int i = 0; i < rows.size(); i++) {
                SearchBean searchBean = JSON.parseObject(rows.getJSONObject(0).toJSONString(), SearchBean.class);
                list.add(searchBean);
            }
            adapter.setNewData(list);
        }
    }
}
